//
//  PayStatusViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/29.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "PayStatusViewController.h"
#import "OrderDetailTableViewCell.h"
#import "OrderDetailViewController.h"
#import "PayStatusModel.h"
#import "MineOrderViewController.h"

@interface PayStatusViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *statusImage;
@property (weak, nonatomic) IBOutlet UILabel *statusTitle;
@property (weak, nonatomic) IBOutlet UILabel *statusMessage;

@property (weak, nonatomic) IBOutlet UIView *addressView;

@property (weak, nonatomic) IBOutlet UILabel *priceTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIButton *checkOrderButton;
@property (weak, nonatomic) IBOutlet UIButton *returnRootButton;

@property (nonatomic, strong) PayStatusModel *detailModel;
@property (nonatomic, strong) OrderDetailTableViewCell *addressCell;
@end

@implementation PayStatusViewController
#pragma mark - IBAction
- (IBAction)checkOrderButtonAction:(id)sender {
//    OrderModel *temp = [[OrderModel alloc] init];
//    temp.osid = self.osid;
//    OrderDetailViewController *orderDetailVC = [[OrderDetailViewController alloc] init];
//    orderDetailVC.detailModel = temp;
//    orderDetailVC.fromType = FromTypeStatus;
//    [self.navigationController pushViewController:orderDetailVC animated:YES];
    MineOrderViewController *mineOrderList = [[MineOrderViewController alloc] init];
    mineOrderList.type = MineOrderTypeAll;
    [self.navigationController pushViewController:mineOrderList animated:YES];
}
- (IBAction)returnRootButtonAction:(id)sender {
    [self disMissSelf];
}
#pragma mark - UIViewController
- (instancetype)init {
    self = [super init];
    if (self) {
        _isIntegral = NO;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"支付结果";
    
    [self createUI];
    [self getNetData];
}
- (void)disMissSelf {
    [self popToRootViewController];
}
#pragma mark - action
- (void)createUI {
    NSString *imageName, *statusTitle, *statusMessage;
    switch (self.type) {
        case StatusTypePaySuccess:
        {
            imageName = @"success";
            statusTitle = @"支付成功！";
            statusMessage = @"包裹正整装待发！请耐心等待~";
        } break;
        case StatusTypePayFail:
        {
            imageName = @"fail";
            statusTitle = @"支付失败！";
            statusMessage = @"支付失败，请重新支付！";
        } break;
        case StatusTypeRefund:
        {
            imageName = @"fail";
            statusTitle = @"退款成功！";
            statusMessage = @"退款成功，请注意查收";
        } break;
        default:
            break;
    }
    [self.statusImage setImage:[UIImage imageNamed:imageName]];
    [self.statusTitle setText:statusTitle];
    [self.statusMessage setText:statusMessage];
    
    self.addressCell = [[[NSBundle mainBundle] loadNibNamed:@"OrderDetailTableViewCell" owner:self options:nil] objectAtIndex:1];
    [self.addressCell.contentView setFrame:CGRectMake(0, 0, ScreenWidth, self.addressView.mj_h)];
    [self.addressView addSubview:self.addressCell.contentView];
    
    [self.checkOrderButton setCircleWithColor:[UIColor grayTextColor] borderWidth:1 cornerRadius:self.checkOrderButton.mj_h/2];
    [self.returnRootButton setCircleWithColor:[UIColor ThemeColor] borderWidth:1 cornerRadius:self.returnRootButton.mj_h/2];
    
    if (self.isIntegral) {
        [self.priceTitleLabel setText:@"支付积分："];
    }
}
- (void)uploadUI {
    [self.addressCell.secondNameLabel setText:[NSString stringWithFormat:@"收货人：%@",self.detailModel.deliveryname]];
    [self.addressCell.secondPhoneLabel setText:[LXLIsPhoneNumber phoneNumber:self.detailModel.deliverytel]];
    [self.addressCell.secondAddressLabel setText:self.detailModel.deliveryaddress];
    
    [self.priceLabel setText:[NSString stringWithFormat:@"%@",self.detailModel.omprice]];
}

- (void)getNetData {
    // 根据订单id获取订单详细信息
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Get_Order_Detail parameters:@{@"omid":self.osid} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            self.detailModel = [PayStatusModel mj_objectWithKeyValues:responseObject[@"data"]];
            [self uploadUI];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
